from sanic.views import HTTPMethodView
from sanic.response import text,json
import time
from processing.other_func import ProToken,Sercret_all,Serialization,Random_id,date_format
import json as jn
import re
from processing.Sql_DataFrom import ProJosn
class wechatList(HTTPMethodView):
    async def get(self, request):
        db = request.app.ctx.db
        # 获取用户列表
        user_id = request.app.ctx.user_id
        client_ip = request.ip

        plugin_id = request.args.get('plugin_id') or ''
        try:
            # 查询商家有没有主题
            sql = """
                       select id,openid,nickName,age,gender,head_img,phone,config,status,create_time from c_users  where merchant_id=%s
                   """
            sql += " ORDER BY create_time DESC "
            data = db.find_all(sql, (user_id))
            if data:
                data = ProJosn(data, sql).to_dict(orient='records')
            else:
                data = []
            return json({'code': 200, 'message': "请求成功!", 'data': data})

        except Exception as error:
            error_sql = 'insert into error_log(visit_ip,error_path,error_content,create_time ) VALUES (%s,%s,%s,now())'
            db.insert(error_sql, (client_ip, '/theme/list', error))
            data = {
                'code': 500,
                'message': '后端报错!'
            }
            return json(data)
class wechatDelete(HTTPMethodView):
    async def post(self, request):
        db = request.app.ctx.db
        # 获取用户列表
        user_id = request.app.ctx.user_id
        client_ip = request.ip

        html_data = request.json

        try:

            sql = "delete from c_users where id=%s"

            db.delete(sql, (html_data['id']))

            return json({'code': 200, 'message': "删除成功!"})

        except Exception as error:
            error_sql = 'insert into error_log(visit_ip,error_path,error_content,create_time ) VALUES (%s,%s,%s,now())'
            db.insert(error_sql, (client_ip, '/wechatApi/wechatDelete', error))
            data = {
                'code': 500,
                'message': '后端报错!'
            }
            return json(data)